package com.joey.cloud.psn.business.service.impl;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.joey.cloud.communal.constant.BaseConstant;
import com.joey.cloud.communal.dto.UserAuthDto;
import com.joey.cloud.psn.business.entity.SysGroup;
import com.joey.cloud.psn.business.entity.SysPerson;
import com.joey.cloud.psn.business.entity.SysRole;
import com.joey.cloud.psn.business.mapper.SysGroupMapper;
import com.joey.cloud.psn.business.mapper.SysPersonMapper;
import com.joey.cloud.psn.business.mapper.SysRoleMapper;
import com.joey.cloud.psn.business.service.ISysGroupService;
import com.joey.cloud.psn.business.service.ISysPersonService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.joey.cloud.psn.business.service.ISysRoleService;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

/**
 * <p>
 * 用户信息 服务实现类
 * </p>
 *
 * @author Joey
 * @since 2021-03-19
 */
@Service
public class SysPersonServiceImpl extends ServiceImpl<SysPersonMapper, SysPerson> implements ISysPersonService {
    @Resource
    ISysGroupService sysGroupService;
    @Resource
    ISysRoleService sysRoleService;
    @Resource
    SysPersonMapper sysPersonMapper;
    @Resource
    SysGroupMapper sysGroupMapper;
    @Resource
    SysRoleMapper sysRoleMapper;

    /**
     * 根据用户userIds获取用户名称
     * @param userIds
     * @return userNames
     */
    @Cacheable(value = "SysPerson",key = "#userIds")
    @Override
    public String queryNameById(String userIds) {
        if(StrUtil.isEmpty(userIds)){
            return null;
        }
        List<String> userIdList = StrUtil.split(userIds, BaseConstant.SPITS_CHAR1);
        List<String> userNameList = new ArrayList<>(userIdList.size());
        QueryWrapper qw = new QueryWrapper();
        qw.select("psnName");
        qw.in("id",userIdList);
        List<SysPerson> list = sysPersonMapper.selectList(qw);
        for(SysPerson psn : list){
            if(!userNameList.contains(psn.getPsnName())){
                userNameList.add(psn.getPsnName());
            }
        }
        return CollUtil.join(userNameList,BaseConstant.SPITS_STR1);
    }

    @Override
    public UserAuthDto queryPsnAuthInfo(Long psnId) {
        UserAuthDto dto = new UserAuthDto();
        dto.setPsnId(psnId);
        List<SysGroup> groupList =  sysGroupMapper.queryListByPsnId(psnId);
        List<SysRole> roleList1 = sysRoleMapper.queryListByPsnId(psnId);
        if(groupList!=null&&groupList.size()>0){
            List<SysRole> roleList2 = sysRoleMapper.queryListByGroupList(groupList);
            roleList1.addAll(roleList2);
            Set<String> groupKeySet = sysGroupService.buildGroupKeySet(groupList);
            dto.setGroupList(groupKeySet);
        }
        Set<String> roleKeySet = sysRoleService.buildRoleKeySet(roleList1);
        dto.setRoleList(roleKeySet);
        return dto;
    }
}
